<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 
/**
 * 手机短信
 */
class Func {
	
    /**
     * 生成注册认证邮件内容
     */
    function regEmailMsg($data = array()){
    	$user_id = $data['user_id'];
		$username = $data['username'];
		$webname = $data['webname'];
		$email = $data['email'];
		$str = implode(',',array($user_id,$username,$email));
		$active_id = urlencode(self::authcode($str,"ENCODE"));
		$_url = "http://{$_SERVER['HTTP_HOST']}/reg/activation?act={$active_id}";
		$send_email_msg = '
		<div style="font-size:14px; width: 588px; ">
		<div>
			<h1 style="padding: 0px 15px; margin: 0px; overflow: hidden; height: 71px;">
				<a title="'.$webname.'用户中心" href="http://'.$_SERVER['HTTP_HOST'].'/user/center/index.html" target="_blank" swaped="true">
				<img style="border-width: 0px; padding: 0px; margin: 0px;" alt="'.$webname.'用户中心" src="http://'.$_SERVER['HTTP_HOST'].'/static/images/logo.png" height="71" width="221">		</a>
			</h1>
			<div style="padding: 0px 20px; overflow: hidden; line-height: 40px; height: 50px; text-align: right;"> </div>
			<div style="padding: 2px 20px 30px;">
				<p>亲爱的会员：<span style="color: rgb(196, 0, 0);">'.$username.'</span> , 您好！</p>
				<p>感谢您注册'.$webname.'账号，您登录的邮箱帐号为 <strong style="font-size: 16px;">'.$email.'</strong></p>
				<p>请点击下面的链接即可完成激活。</p>
				<p style="overflow: hidden; width: 100%; word-wrap: break-word;"><a title="点击完成注册" href="'.$_url.'" target="_blank" swaped="true">'.$_url.'</a>
				<br><span style="color: rgb(153, 153, 153);">(如果链接无法点击，请将它拷贝到浏览器的地址栏中)</span></p>
				<p>您在'.CON_PLATFORMNAME.'可以享受到以下服务：</p>
				<p>1、免费发布借款信息，全网络化的信用审核，简便快捷的借到资金。</p>
				<p>2、可以将闲钱通过平台出借给那些需要借款的人，'.CON_PLATFORMNAME.'担保您的资金安全，然后每月回收本金和利息，从而获取稳定的投资回报，贷款的利息收益远高于存银行的利息。</p>
				<p>我们希望您能在'.CON_PLATFORMNAME.'上得到更多人的帮助，也希望您能在'.CON_PLATFORMNAME.'上帮助更多的人，并从中获得乐趣！</p>
				<p>感谢您的支持！</p>
				<p>'.CON_PLATFORMNAME.'团队</p>
				<p>注：此邮件由系统自动发送，请勿回复</p>
				<p>如果您有任何疑问，可以点击<a title="点击联系我们" style="color: rgb(15, 136, 221);" href="http://'.$_SERVER['HTTP_HOST'].'/kefu/index/index.html" target="_blank" >这里</a>联系我们的客服</p>
				
		</div>
		</div>
	</div>
			';
		return $send_email_msg;
    }
    
	function findPwdMail($data){
    	$user_id = $data['user_id'];
		$username = $data['username'];
		$webname = $data['webname'];
		$email = $data['email'];
		$str = implode(',',array(time(),$user_id,$username,$email));
		$active_id = urlencode(self::authcode($str,"ENCODE",'',3600));
		$_url = "http://{$_SERVER['HTTP_HOST']}/password/getpwd?check=".$active_id;
		
		$send_email_msg = '
		<div style=" font-size:14px; width: 588px; ">
		<div>
			<h1 style="padding: 0px 15px; margin: 0px; overflow: hidden; height: 71px;">
				<a title="'.$webname.'用户中心" href="http://'.$_SERVER['HTTP_HOST'].'/user/center/index.html" target="_blank" swaped="true">
				<img style="border-width: 0px; padding: 0px; margin: 0px;" alt="'.$webname.'用户中心" src="http://'.$_SERVER['HTTP_HOST'].'/static/images/logo.png" height="71" width="221">		</a>
			</h1>
			<div style="padding: 0px 20px; overflow: hidden; line-height: 40px; height: 50px; text-align: right;"> </div>
			<div style="padding: 2px 20px 30px;">
				<p>亲爱的'.$webname.'会员：<span style="color: rgb(196, 0, 0);">'.$username.'</span> , 您好！</p>
				<p>请点击下面的链接完成登陆密码找回操作。</p>
				<p style="overflow: hidden; width: 100%; word-wrap: break-word;"><a title="点击找回密码" href="'.$_url.'" target="_blank" swaped="true">'.$_url.'</a>
				<br><span style="color: rgb(153, 153, 153);">(如果链接无法点击，请将它拷贝到浏览器的地址栏中)</span></p>
				<p>该链接有效时间1小时，过期作废</p>
				<p>注：此邮件由系统自动发送，请勿回复</p>
				<p>如果您有任何疑问，可以点击<a title="点击联系我们" style="color: rgb(15, 136, 221);" href="http://'.$_SERVER['HTTP_HOST'].'/kefu/index/index.html" target="_blank" >这里</a>联系我们的客服</p>

		</div>
		</div>
	</div>
			';
		return $send_email_msg;
    }
    
    function findPaypwdMail($data){
    	$user_id = $data['user_id'];
		$username = $data['username'];
		$webname = $data['webname'];
		$email = $data['email'];
		$str = implode(',',array($user_id,$username,$email,time()));
		$active_id = urlencode(self::authcode($str,"ENCODE",'',3600));
		$_url = "http://{$_SERVER['HTTP_HOST']}/password/paypwd?check=".$active_id;
		
		$send_email_msg = '
		<div style="font-size:14px; width: 588px; ">

			<h1 style="padding: 0px 15px; margin: 0px; overflow: hidden; height: 71px;">
				<a title="'.$webname.'用户中心" href="http://'.$_SERVER['HTTP_HOST'].'/user/center/index.html" target="_blank" swaped="true">
				<img style="border-width: 0px; padding: 0px; margin: 0px;" alt="'.$webname.'用户中心" src="http://'.$_SERVER['HTTP_HOST'].'/static/images/logo.png" height="71" width="221">		</a>
			</h1>
			<div style="padding: 0px 20px; overflow: hidden; line-height: 40px; height: 50px; text-align: right;"> </div>
			<div style="padding: 2px 20px 30px;">
				<p>亲爱的'.$webname.'会员：<span style="color: rgb(196, 0, 0);">'.$username.'</span> , 您好！</p>
				<p>请点击下面的链接完成交易密码找回操作。</p>
				<p style="overflow: hidden; width: 100%; word-wrap: break-word;"><a title="点击找回交易密码" href="'.$_url.'" target="_blank" swaped="true">'.$_url.'</a>
				<br><span style="color: rgb(153, 153, 153);">(如果链接无法点击，请将它拷贝到浏览器的地址栏中)</span></p>
				<p>该链接有效时间1小时，过期作废</p>
				<p>注：此邮件由系统自动发送，请勿回复</p>
				<p>如果您有任何疑问，可以点击<a title="点击联系我们" style="color: rgb(15, 136, 221);" href="http://'.$_SERVER['HTTP_HOST'].'/help/index.html" target="_blank" >这里</a>联系我们的客服</p>

		</div>

	</div>
			';
		return $send_email_msg;
    }
    
    
    function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
		// 动态密匙长度，相同的明文会生成不同密文就是依靠动态密匙
		$ckey_length = 4;
		// 密匙
		$key = md5($key ? $key : "gsdai0427");
		// 密匙a会参与加解密
		$keya = md5(substr($key, 0, 16));
		// 密匙b会用来做数据完整性验证
		$keyb = md5(substr($key, 16, 16));
		// 密匙c用于变化生成的密文
		$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
		// 参与运算的密匙
		$cryptkey = $keya.md5($keya.$keyc);
		$key_length = strlen($cryptkey);
		// 明文，前10位用来保存时间戳，解密时验证数据有效性，10到26位用来保存$keyb(密匙b)，解密时会通过这个密匙验证数据完整性
		// 如果是解码的话，会从第$ckey_length位开始，因为密文前$ckey_length位保存 动态密匙，以保证解密正确
		$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
		$string_length = strlen($string);
		$result = '';
		$box = range(0, 255);
		$rndkey = array();
		
		// 产生密匙簿
		for($i = 0; $i <= 255; $i++) {
			$rndkey[$i] = ord($cryptkey[$i % $key_length]);
		}
		
		// 用固定的算法，打乱密匙簿，增加随机性，好像很复杂，实际上对并不会增加密文的强度
		for($j = $i = 0; $i < 256; $i++) {
			$j = ($j + $box[$i] + $rndkey[$i]) % 256;
			$tmp = $box[$i];
			$box[$i] = $box[$j];
			$box[$j] = $tmp;
		}
		
		// 核心加解密部分
		for($a = $j = $i = 0; $i < $string_length; $i++) {
			$a = ($a + 1) % 256;
			$j = ($j + $box[$a]) % 256;
			$tmp = $box[$a];
			$box[$a] = $box[$j];
			$box[$j] = $tmp;
			// 从密匙簿得出密匙进行异或，再转成字符
			$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
		}
		
		
		if($operation == 'DECODE') {
			// substr($result, 0, 10) == 0 验证数据有效性
			// substr($result, 0, 10) - time() > 0 验证数据有效性
			// substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16) 验证数据完整性
			// 验证数据有效性，请看未加密明文的格式
			if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
				return substr($result, 26);
			} else {
				return '';
			}
		} else {
			// 把动态密匙保存在密文里，这也是为什么同样的明文，生产不同密文后能解密的原因
			// 因为加密后的密文可能是一些特殊字符，复制过程可能会丢失，所以用base64编码
			return $keyc.str_replace('=', '', base64_encode($result));
		}
		
	}

	function protocol_msg($borrow_id, $tender_id = 0){
		if($borrow_id > 0){
			if($tender_id > 0){
				$url = "http://{$_SERVER['HTTP_HOST']}/borrow/lz_protocol/{$borrow_id}/$tender_id/";	
			}else{
				$url = "http://{$_SERVER['HTTP_HOST']}/borrow/protocol/{$borrow_id}";
			}
			
			$content =  file_get_contents($url);
			$a = "<div class=\"warp\" style=\"width:900px;margin:0 auto;overflow:hidden;padding-right:100px;line-height:50px;\">";
			$arr = explode($a, $content);
			$content = $arr[1];
			$a = "<div class=\"clear\"></div>";
			$arr = explode($a, $content);
			$content = $arr[0];
			
			$temp=$content;
		   	$start = "<script>";
			$end = "</script>"; 
		   	while(strpos($temp, $start) && strpos($temp, $end)){  
			    $temp=substr($temp, 0, strpos($temp, $start)).substr($temp,strpos($temp, $end)+strlen($end));
		   	}
			$send_email_msg = $temp;
			return $send_email_msg;
		}else{
			return '';
		}
	}
	
	function yjb_pro_msg($borrow_id, $tender_id,$uid=0){
		$uid = intval($uid);
		$url = "http://{$_SERVER['HTTP_HOST']}/borrow/sl_protocol/{$borrow_id}/{$tender_id}.html?u={$uid}";
		$content =  file_get_contents($url);
		$a = "<div class=\"warp\" style=\"width:900px;margin:0 auto;overflow:hidden;padding-right:100px;line-height:50px;\">";
		$arr = explode($a, $content);
		$content = $arr[1];
		$a = "<div class=\"gsdfoot\">";
		$arr = explode($a, $content);
		$content = $arr[0];
		
		$temp=$content;
	   	$start = "<script>";
		$end = "</script>"; 
	   	while(strpos($temp, $start) && strpos($temp, $end)){  
		    $temp=substr($temp, 0, strpos($temp, $start)).substr($temp,strpos($temp, $end)+strlen($end));
	   	}
		$send_email_msg = $temp;
		return $send_email_msg;
	}
	
	/**
	 * 掷骰子概率计算
	 * @param array $arr array('a'=>20,'b'=>30,'c'=>50) 
	 * @return bool/string 返回数组的KEY
	 */
	function dice($rateArr)
	{
		$ret = false;
		$arr = array();
		$tmpMaxVal = 0;
		$tmpMaxKey = '';
		if (!empty($rateArr))
		{
			$max = array_sum($rateArr);
			$input = range(1, $max);
			$one = rand(1, $max);
			shuffle($input);
	
			foreach ($rateArr as $key => $val)
			{
				$val = !empty($val) ? intval($val) : 0;
				if ($val > $tmpMaxVal)
				{
					$tmpMaxVal = $val;
					$tmpMaxKey = $key;
				}
				$tmpInput = array_splice($input, $val);
				$spArr = $input;
				$input = $tmpInput;
	
				foreach ($spArr as $n)
				{
					$arr[$n] = $key;
				}
			}
			
			$ret = isset($arr[$one])?$arr[$one]:$tmpMaxKey;
		}
		return $ret;
	}
	
	function array_sort($arr,$keys,$type='asc'){
		$keysvalue = $new_array = array();
		foreach ($arr as $k=>$v){
			$keysvalue[$k] = $v[$keys];
		}
		if($type == 'asc'){
			asort($keysvalue);
		}else{
			arsort($keysvalue);
		}
		reset($keysvalue);
		foreach ($keysvalue as $k=>$v){
			$new_array[$k] = $arr[$k];
		}
		return $new_array;
	} 
	
	function protocol_msg_creditor($creditor_id,$user_id,$id){
		$url = "http://{$_SERVER['HTTP_HOST']}/userborrow/creditor_protocol/{$creditor_id}/{$user_id}/{$id}";
		
		$content =  file_get_contents($url);
		$a = "<div class=\"warp\" style=\"width:900px;margin:0 auto;overflow:hidden;padding-right:100px;line-height:50px;\">";
		$arr = explode($a, $content);
		$content = $arr[1];
		$a = "<div class=\"clear\"></div>";
		$arr = explode($a, $content);
		$content = $arr[0];
		
		$temp=$content;
	   	$start = "<script>";
		$end = "</script>"; 
	   	while(strpos($temp, $start) && strpos($temp, $end)){  
		    $temp=substr($temp, 0, strpos($temp, $start)).substr($temp,strpos($temp, $end)+strlen($end));
	   	}
		$send_email_msg = $temp;
		return $send_email_msg;
		
	}
	
    
}